﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class UdfGridReportSettingBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IUdfGridReportSettingDA udfGridReportSettingDA = DataAccess.CreateUdfGridReportSettingDA();
        private static readonly IUdfGridReportSettingDetailDA udfGridReportSettingDetailDA = DataAccess.CreateUdfGridReportSettingDetailDA();

        public UdfGridReportSettingBLL()
        {
            this.pubParasInfo = null;
        }

        public UdfGridReportSettingBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UdfGridReportSettingBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<UdfGridReportSettingInfo> udfReportSettingList = new List<UdfGridReportSettingInfo>();
                UdfGridReportSettingInfo udfGridReportSettingInfo = null;
                for (i = 0; i < IDS.Count; i++)
                {
                    udfGridReportSettingInfo = udfGridReportSettingDA.GetUdfGridReportSettingByID(dataBase, tran, IDS[i], this.dataBaseID[0]);
                    udfReportSettingList.Add(udfGridReportSettingInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                udfGridReportSettingDetailDA.DeleteUdfGridReportSettingDetail(dataBase, tran, IDS, this.dataBaseID[0]);
                udfGridReportSettingDA.DeleteUdfGridReportSetting(dataBase, tran, IDS, this.dataBaseID[0]);
                for (i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<UdfGridReportSettingInfo>(dataBase, tran, udfReportSettingList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "UDF_REPORT_SETTING", this.dataBaseID);
                    if ((udfReportSettingList[i].UdfGridReportSettingDetailInfos != null) && (udfReportSettingList[i].UdfGridReportSettingDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < udfReportSettingList[i].UdfGridReportSettingDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<UdfGridReportSettingDetailInfo>(dataBase, tran, udfReportSettingList[i].UdfGridReportSettingDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ";";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return udfGridReportSettingDA.GetAllUdfGridReportSetting(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            UdfGridReportSettingInfo udfGridReportSettingInfo = udfGridReportSettingDA.GetUdfGridReportSettingByID(id, this.dataBaseID[0]);
            if (udfGridReportSettingInfo != null)
            {
                udfGridReportSettingInfo.UdfGridReportSettingDetailInfos = udfGridReportSettingDetailDA.GetUdfGridReportSettingDetailListByID(id, this.dataBaseID[0], this.pubParasInfo);
            }
            return udfGridReportSettingInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            UdfGridReportSettingQueryEntity udfGridReportSettingQueryEntity = obj as UdfGridReportSettingQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(udfGridReportSettingQueryEntity.ReportID))
            {
                sqlWhere.Add(" REPORT_ID " + udfGridReportSettingQueryEntity.ReportID);
            }
            if (!string.IsNullOrEmpty(udfGridReportSettingQueryEntity.ReportName))
            {
                sqlWhere.Add(" REPORT_NAME " + udfGridReportSettingQueryEntity.ReportName);
            }
            return udfGridReportSettingDA.GetUdfGridReportSettingByQueryList(sqlWhere, udfGridReportSettingQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            UdfGridReportSettingInfo udfGridReportSettingInfo = obj as UdfGridReportSettingInfo;
            try
            {
                if (string.IsNullOrEmpty(udfGridReportSettingInfo.ReportID))
                {
                    udfGridReportSettingInfo.ReportID = commonMethod.GenNextNumStr(dataBase, tran, "UDF_GRID_REPORT_SETTING", this.dataBaseID[0]);
                }
                else if (!string.IsNullOrEmpty(udfGridReportSettingInfo.ReportID) && !udfGridReportSettingDA.CheckUdfGridReportSettingIDUnique(dataBase, tran, udfGridReportSettingInfo.ReportID, this.dataBaseID[0]))
                {
                }
                udfGridReportSettingDA.InsertUdfGridReportSetting(dataBase, tran, udfGridReportSettingInfo, this.dataBaseID[0]);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<UdfGridReportSettingInfo>(dataBase, tran, null, udfGridReportSettingInfo, AuditAction.Insert, this.pubParasInfo.UserID, "UDF_REPORT_SETTING", this.dataBaseID);
                if ((udfGridReportSettingInfo.UdfGridReportSettingDetailInfos != null) && (udfGridReportSettingInfo.UdfGridReportSettingDetailInfos.Count > 0))
                {
                    for (int i = 0; i < udfGridReportSettingInfo.UdfGridReportSettingDetailInfos.Count; i++)
                    {
                        if (udfGridReportSettingDetailDA.CheckUdfGridReportSettingDetailIDUnique(dataBase, tran, udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].ReportID, udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F1870_001", this.pubParasInfo.UserLanguage, new List<string> { udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].ReportID, udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].LineID.ToString() });
                        }
                        else
                        {
                            udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].CreatedDate = DateTime.Now;
                            udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].UpdatedDate = DateTime.Now;
                            udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                            udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                            udfGridReportSettingDetailDA.InsertUdfGridReportSettingDetail(dataBase, tran, udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i], this.dataBaseID[0]);
                            auditDataBLL.InsertAuditData<UdfGridReportSettingDetailInfo>(dataBase, tran, null, udfGridReportSettingInfo.UdfGridReportSettingDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return udfGridReportSettingInfo.ReportID;
        }

        public string Update(object obj)
        {
            string strReportID = string.Empty;
            try
            {
                UdfGridReportSettingInfo udfGridReportSettingInfo = obj as UdfGridReportSettingInfo;
                UdfGridReportSettingInfo oldudfreportsettingInfo = udfGridReportSettingDA.GetUdfGridReportSettingByID(udfGridReportSettingInfo.ReportID, this.dataBaseID[0]);
                strReportID = udfGridReportSettingInfo.ReportID;
                if (udfGridReportSettingDA.UpdateUdfGridReportSetting(udfGridReportSettingInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UdfGridReportSettingInfo>(oldudfreportsettingInfo, udfGridReportSettingInfo, AuditAction.Update, this.pubParasInfo.UserID, "UDF_REPORT_SETTING", this.dataBaseID[0]);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return strReportID;
        }
    }
}

